package database;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;

import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

/**
 * This class represent Supermarket for Hibernate ORM Library
 * @author Moshe Ronen, Lior Bunzl
 *
 */
@Entity
@Table(name = "PRODUCT", catalog = "telhai", uniqueConstraints = {
			@UniqueConstraint(columnNames = {"PRODUCT_NAME","PRODUCT_UNIT", "PRODUCT_QUANTITY_PER_UNIT"})})
public class Product implements Serializable
{
	private static final long serialVersionUID = 1L;
	private Integer productId;
    private String productName;
    private String productUnit;
    private Double productQuantityPerUnit;
    private String productImage;
	private Set<ProductSupermarket> productSupermarkets = new HashSet<ProductSupermarket>(0);
	
	public Product()
	{}
	
	public Product(String productName, String productUnit, Double productQuantityPerUnit, String productImage)
	{
		this.productName = productName;
		this.productUnit = productUnit;
		this.productQuantityPerUnit = productQuantityPerUnit;
		this.productImage = productImage;
	}
	
	public Product(String productName, String productUnit, double productQuantityPerUnit, String productImage, Set<ProductSupermarket> productSupermarkets)
	{
		this.productName = productName;
		this.productUnit = productUnit;
		this.productQuantityPerUnit = productQuantityPerUnit;
		this.productImage = productImage;
		this.productSupermarkets = productSupermarkets;
	}

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "PRODUCT_ID", unique = true, nullable = false)
	public Integer getProductId()
	{
		return this.productId;
	}

	public void setProductId(Integer productId)
	{
		this.productId = productId;
	}

	@Column(name = "PRODUCT_NAME", unique = true, nullable = false, length = 50)
	public String getProductName()
	{
		return this.productName;
	}

	public void setProductName(String productName)
	{
		this.productName = productName;
	}

	@Column(name = "PRODUCT_UNIT", unique = false, nullable = false, length = 20)
	public String getProductUnit()
	{
		return this.productUnit;
	}

	public void setProductUnit(String productUnit)
	{
		this.productUnit = productUnit;
	}

	@Column(name = "PRODUCT_QUANTITY_PER_UNIT", unique = false, nullable = false)
	public Double getProductQuantityPerUnit()
	{
		return this.productQuantityPerUnit;
	}

	public void setProductQuantityPerUnit(Double productQuantityPerUnit)
	{
		this.productQuantityPerUnit = productQuantityPerUnit;
	}

	@Column(name = "PRODUCT_IMAGE", unique = false, nullable = true, length = 255)
	public String getProductImage()
	{
		return this.productImage;
	}

	public void setProductImage(String productImage)
	{
		this.productImage = productImage;
	}

	@OneToMany(fetch = FetchType.LAZY, mappedBy = "pk.product")
	public Set<ProductSupermarket> getProductSupermarkets()
	{
		return this.productSupermarkets;
	}

	public void setProductSupermarkets(Set<ProductSupermarket> productSupermarkets)
	{
		this.productSupermarkets = productSupermarkets;
	}
	
	@Override
	public String toString()
	{   
	    return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE)
	    .append("Id", productId)
	    .append("Name", productName)
	    .append("Unit", productUnit)
	    .append("Quantity Per Unit", productQuantityPerUnit.toString())
	    .append("Image", productImage)
	    .toString();
	}
}
